const fs = require('fs')
const path = require("path");

function saveVideo(file, blogUserId = null) {
    return new Promise((resolve, reject) => {
        try {
            if (!blogUserId) {
                blogUserId = '_'
            }
            //创建文件的存储路径，并对文件进行重命名
            const audioSuffix = file.originalFilename.split('.')[1]  //截取后缀
            const videoTime = new Date().getTime()
            let filePath = path.join(__dirname, '../public/video/') + videoTime + blogUserId + '.' + audioSuffix;  //拼接路径
            //读取文件，返回文件流
            const rs = fs.createReadStream(file.filepath);
            const rs2 = fs.createWriteStream(filePath)
            rs.pipe(rs2); //开启管道流
            //监听成功
            rs2.on('finish', () => {
                resolve(filePath)
            })

        } catch (e) {
            reject(e)
        }
    })

}


module.exports = saveVideo
